home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15895 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  96 lines

  1. Path: trib.apple.com!usenet
  2. From: Amir <Amir@bayarea.net>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [HELP!!!] Segmentation Fault!
  5. Date: Mon, 08 Apr 1996 11:04:30 -0700
  6. Organization: Apple Computer, Inc., Cupertino, California
  7. Message-ID: <3169552E.B49@bayarea.net>
  8. References: <4k4gja$mm@nuscc.nus.sg>
  9. NNTP-Posting-Host: a17-128-200-53.apple.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (Macintosh; I; PPC)
  14.  
  15. Hi
  16.  
  17. I just stopped on the first major prob I found.
  18.  
  19. Virus wrote:
  20. > ...
  21. >
  22. > char* SpaceStudentFile::readNextField()
  23. > {
  24. >   char str[40];
  25. >   int i = 0;
  26. >   // take away starting whitespaces
  27. >   while (str[0] = fgetc(fd)) {
  28. >         if (*str == ' ') continue;
  29. >         if (*str == '\t') continue;
  30. >         if (*str == '\n') continue;
  31. >         if (*str == EOF) return NULL;
  32. >         break;
  33. >   }
  34. >   // read from " to next "
  35. >   if (str[0] == '"') {
  36. >         for (i=0; ((str[i] = fgetc(fd)) != '"'); i++)
  37. >         ;
  38. >         str[i]=0;
  39. >         return str;
  40. >   }
  41. >   // else read until next whitespace
  42. >   for (i=1; ((str[i] = fgetc(fd)) != ' ')
  43. >         && (str[i] != '\n') && (str[i] != '\t'); i++)
  44. >   ;
  45. >   str[i] = 0;
  46. >   return str;
  47. > }
  48. > ....
  49.  
  50. str is a local variable, that means it is created when you
  51. call this method, and removed when the function returns to its
  52. caller, so when you do "return str" you actually return a pointer
  53. to a non existing string.
  54. There are several valid solutions, one of them is to allocate
  55. memory for it like:
  56.  
  57. char* SpaceStudentFile::readNextField()
  58. {
  59.  char str[40];
  60.  char * newStr;
  61.  
  62.  ....
  63.  
  64.  str[i] = 0;
  65.  newStr = strdup(str); // If your system doesn't have strdup you can use
  66.                        // newStr = malloc(strlen(str) + 1);
  67.                        // strcpy(newStr, str);
  68.  return newStr;
  69. }
  70.  
  71. This is a simple solution that will work, but this is not a very
  72. good one (you need to remember to free the memory when you
  73. don't need it anymore, and this is the C way, not C++). If you have 
  74. some time to spend on this, than try to use in your classes the String 
  75. class instead of "char *"
  76.  
  77. Amir
  78.  
  79.  
  80. -- 
  81.  
  82.                     \\||||//
  83.                     \ o  o /
  84. ______________.ooO__(  ()  )__Ooo._______________________________
  85.  
  86.    Amir Deutel                  http://www.bayarea.net/~amir/
  87.  
  88.                  .oo0O   O0oo.  Work: Amir.D@AppleLink.Apple.com
  89.                  (   )   (   )  Home: Amir@bayarea.net
  90. __________________\ (_____) /____________________________________
  91.                    \_)   (_/
  92.